{% extends "layout.html" %}
{% block body %}

<h2>编辑</h2>
  <form >
    <input type="hidden" id="articleId" name="articleId" value="{{article.id}}"/>
    <div class="form-group">
      <label for="name">标题:</label>
      <input type="text" class="form-control" id="name" name="name" value="{{article.name}}">
    </div>
    <div class="form-group">
       <label for="contents">内容:</label>
        <textarea class="form-control" rows="5" id="contents" name="contents">{{article.contents}}</textarea>
    </div>
      <button type="button" onclick="btnSave()" class="btn btn-primary">Submit</button>
      <button type="button" onclick="javascript:window.location.href='{{url_for('article.index')}}'" class="btn">Goback</button>
  </form>

<script>
    function btnSave(){
        var name=$.trim($("#name").val());
        var contents=$.trim($("#contents").val());
        var id=$("#articleId").val();
        if(name==''){
            alert("请输入标题");
            return false;
        }

        var paramData={
            name:name,
            contents:contents
        }
        $.ajax({
            type:'POST',
            url:'/article/edit/'+id,
            data:paramData,
            dataType:'json',
            success:function(data){
                if(data.code=="succ"){
                    window.location.reload();
                    alert(data.msg);
                }
                else{
                    alert(data.msg);
                }
            }
        });
    }
</script>
{% endblock %}
